﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Nomad.WebSync.Windows.Forms {
    /// <summary>
    /// Interaction logic for Main.xaml
    /// </summary>
    public partial class Main : Window {
        public Main() {
            InitializeComponent();
        }

        private void allowAccess() {
            if (mainFrame.Source == null) {
                var online = (App.WebSyncHeader != null);
                startIcon.IsHitTestVisible = online;
                newMenuItem.IsHitTestVisible = online;
            }
        }

        private void startIcon_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {            
            this.Cursor = Cursors.Wait;
            try {
                (sender as Image).ContextMenu.IsOpen = true;
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
        private void mainFrame_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e) {            
            this.Cursor = Cursors.Wait;
            try {
                this.allowAccess();
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
        private void newAccountMenuItem_Click(object sender, RoutedEventArgs e) {
            this.Cursor = Cursors.Wait;
            try {
                mainFrame.Navigate(new Register());
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
        private void settingsMenuItem_Click(object sender, RoutedEventArgs e) {
            this.Cursor = Cursors.Wait;
            try {
                mainFrame.Navigate(new Settings());
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
        private void exitMenuItem_Click(object sender, RoutedEventArgs e) {
            this.Cursor = Cursors.Wait;
            try {
                App.Current.Shutdown();
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            } finally {
                this.Cursor = Cursors.Arrow;
            }
        }
    }
}
